package com.aaa.javaweb.servlet;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller

public class WenJianControcoller {
    @RequestMapping("upload")
    @ResponseBody
    public Map upload(MultipartFile file){
        String originalFilename = file.getOriginalFilename();

        //文件重新命名
        String s = UUID.randomUUID().toString();
        String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
        //文件的名字
        String filename=s+substring;

        String endpoint="oss-cn-beijing.aliyuncs.com";
        String accessKeyId = "LTAI4FzJP8kBb76TmBqQ8BDo";
        String accessKeySecret = "lcFxxcnZrGnZgeHTakworrRKWpnW1v";

        String bucketName = "aaachenjian";

        OSS oss = new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);

        try {
            InputStream inputStream = file.getInputStream();
            PutObjectResult putObjectResult = oss.putObject(bucketName, filename, inputStream);
            //关闭
            oss.shutdown();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String path = "https://" + bucketName + "." + endpoint + "/" + filename;
        System.out.println(path);
        Map map = new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("data",path);
        return map;
    }


}
